<?php

use frame\lib\Db;

function response($data = [], $msg = '获取成功', $code = 200)
{
    header('Content-Type: application/json; charset=utf-8');
    $data = [
        'msg' => $msg,
        'data' => $data,
        'code' => $code
    ];
    exit(json_encode($data, JSON_UNESCAPED_UNICODE));
}
function success($data = [], $msg = "success", $code = 200)
{
    exit(response($data, $msg, $code));
}
function error($msg = "error", $code = 0)
{
    exit(response($data = [], $msg, $code));
}
/**
 * 预防XSS攻击的函数
 *
 * @param string $input 用户输入的数据
 * @param int $flags htmlspecialchars函数的flags参数，默认为 ENT_QUOTES | ENT_HTML401
 * @param string $encoding 编码方式，默认为 UTF-8
 * @return string 过滤后的数据
 */
function preventXSS($input, $flags = ENT_QUOTES | ENT_HTML401, $encoding = 'UTF-8')
{
    // 去除一些常见的恶意字符
    $input = strip_tags($input); // 去除HTML标签
    $input = htmlspecialchars($input, $flags, $encoding); // 将特殊字符转换为HTML实体
    $input = preg_replace('/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]+/u', '', $input); // 去除控制字符

    return $input;
}
function randStr($length = 20)
{
    $str = '';
    $rand = '123456890qwertyuiopasdfghjklzxcvbnm';
    for ($j = 0; $j < $length; $j++) {
        $d = substr($rand, rand(0, strlen($rand) - 1), 1);
        $str .= $d;
    }
    return $str;
}
function ip()
{
    if (getenv('HTTP_CLIENT_IP')) {
        $ip = getenv('HTTP_CLIENT_IP');
    }
    if (getenv('HTTP_X_REAL_IP')) {
        $ip = getenv('HTTP_X_REAL_IP');
    } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
        $ips = explode(',', $ip);
        $ip = $ips[0];
    } elseif (getenv('REMOTE_ADDR')) {
        $ip = getenv('REMOTE_ADDR');
    } else {
        $ip = '0.0.0.0';
    }

    return $ip;
}

function isValidEmail($email)
{
    return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}

/**
 * @param int $type 1 (默认) 取当前时间 2 由时间戳转时间 3 由时间转时间戳
 * @param string $time (时间戳|时间)  
 */
function getTime($type = 1, $time = '')
{
    if ($type == 1) {
        return date('Y-m-d H:i:s', time());
    } else if ($type == 2) {
        if (!is_int($time)) return 0;
        return date("Y-m-d H:i:s", $time);
    } else {
        if (!$time) return 0;
        return strtotime($time);
    }
}
// 密码 md5加密
function encrypt_pwd($pwd)
{
    return md5($pwd . config('secure', 'secure_salt'));
}
// vip_time值计算
function vipTimeCompute($type, $count)
{
    $day = 3600 * 24;
    switch ($type) {
        case 1:
            $vip_time = $day;
            break;
        case 2:
            $vip_time = $day * 30;
            break;
        case 3:
            $vip_time = $day * 365;
            break;
    }
    return $vip_time * $count;
}



/* function request_limit($paramName, $expire, $msg = '')
{
    if (!$msg) {
        $msg = "请等待 $expire 秒后再来请求";
    } 
    $limit = @$_COOKIE[$paramName];
    if ($limit) error($msg);
    setcookie($paramName, true, time() + $expire, '/');
} */

/**
 * --------请求限制--------
 * 必须拥有字段 create_time
 * @param String $table 表名
 * @param String $where 请求条件 uid = 5 || user_id = 6
 * @param int $expire 限制请求时长
 * @param String $msg 请求提示
 */
function request_limit($table, $where , $expire, $msg = '')
{
    $sql = "select create_time as t from $table where id = (select max(id) as id from  $table  where $where);";
    $t = Db::queryOne($sql);
    if($t){
        if (getTime(3, $t['t']) + $expire > time()) {
            if (!empty($msg)) error($msg);
            else  error("请 $expire 后再来请求哈 !");
        }
    }
    
}
